package com.myx.servlet;

import com.myx.pojo.News;
import com.myx.service.news.NewsService;
import com.myx.service.news.NewsServiceImpl;
import com.myx.tool.Constant;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "GetNewsListPagesByCategoryIdServlet",value = "/GetNewsListPagesByCategoryIdServlet")
public class GetNewsListPagesByCategoryIdServlet extends HttpServlet {
    NewsService newsService=new NewsServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");


        String categoryIdStr=request.getParameter("categoryId");
        String pageNoStr=request.getParameter("pageNo");
        Integer categoryId=null;
        Integer currentNo=null;

        if (categoryIdStr != null) {
            categoryId = Integer.parseInt(categoryIdStr);
        }

        if (pageNoStr != null) {
            currentNo = Integer.parseInt(pageNoStr);
            if (currentNo <= 0) {
                currentNo=1;
            }
        }


        List<News> newsListPagesByCategoryId = newsService.getNewsListPagesByCategoryId(categoryId, currentNo, Constant.PAGE_SIZE);

        //请求转发
        request.setAttribute("newsListPagesByCategoryIdKey",newsListPagesByCategoryId);
        request.setAttribute("categoryIdKey",categoryId);
        request.setAttribute("currentNoKey",currentNo);
        request.getRequestDispatcher("/index.jsp").forward(request,response);


    }
}
